cc.Class({
    extends: cc.Component,

    properties: {
        target: {
            default: null,
            type: cc.Node,
            tooltip: '（已废弃）尺寸控制的参考目标'
        },
        minSize: {
            default: new cc.Size(),
            tooltip: '最小尺寸，0为不限制'
        },
        maxSize: {
            default: new cc.Size(),
            tooltip: '最大尺寸，0为不限制'
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

        this.target = this.target || this.node;

        this._thisOnResized = this.onResized.bind(this);

        this._addListeners();

        this.onResized();
    },

    onResized(){

        //
        var curSize = this.node.getContentSize();
        var size = new cc.Size();
        // 根据指定值是否为0，再构造一下尺寸限制
        var minSize = new cc.Size(this.minSize.width || 0, this.minSize.height || 0);
        var maxSize = new cc.Size(this.maxSize.width || Infinity, this.maxSize.height || Infinity);
        //
        size.width = cc.misc.clampf(curSize.width, minSize.width, maxSize.width);
        size.height = cc.misc.clampf(curSize.height, minSize.height, maxSize.height);
        //
        if (curSize.width != size.width || curSize.height != size.height){
            this.node.setContentSize(size);
            cc.log('new size for ' + this.node.name + ': ' + size);
        } else {
            cc.log('size of ' + this.node.name + ': ' + size);
        }
    },

    onDestroy(){
        this._removeListeners();
    },

    _addListeners(){
        if (cc.sys.isMobile) {
            window.addEventListener('resize', this._thisOnResized);
        }
        else {
            cc.view.on('canvas-resize', this._thisOnResized);
        }
    },

    _removeListeners(){
        if (cc.sys.isMobile) {
            window.removeEventListener('resize', this._thisOnResized);
        }
        else {
            cc.view.off('canvas-resize', this._thisOnResized);
        }
    }
});

